﻿using P2PWeb.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace P2PWeb.Controllers
{
    [AccountAuthorize(AccountType.Admin, AccountType.LookThrough, AccountType.User, AccountType.Agent)]
    public class UploadFileController : BaseController
    {
        [HttpPost]
        public JsonResult UploadingFile(object TheFile)
        {
            JsonResult result = null;
            HttpPostedFileBase[] files = (TheFile as HttpPostedFileBase[]);
            if (files == null || files.Length <= 0)
            {
                result = new JsonResult()
                {
                    Data = new { IsFail = true, error = "请选择抵押证明文件" }
                };
                return result;
            }
            HttpPostedFileBase file = files[0];
            if (file.ContentLength > 1048576 * 2)
            {
                result = new JsonResult()
                {
                    Data = new { IsFail = true, error = "抵押证明文件大小不能超过2M" }
                };
                return result;
            }
            if (!(file.ContentType == "image/jpeg" || file.ContentType == "image/png" || file.ContentType == "image/bmp"))
            {
                result = new JsonResult()
                {
                    Data = new { IsFail = true, error = "抵押证明文件的格式必须是 png、bmp、jpg其中之一." }
                };
                return result;
            }
            Guid guid = Guid.NewGuid();
            string fileName = guid.ToString() + System.IO.Path.GetExtension(file.FileName);
            file.SaveAs(Server.MapPath("~/Attachments/") + fileName);
            return new JsonResult() { Data = new { IsSurcess = true, FileName = fileName } };
        }

    }
}
